let webAudioSpeechRecognizer;
let webAudioSpeechRecognizer2;
let isCanStop;
$(function () {
  const params = {
    signCallback: signCallback, // 鉴权函数
    // 用户参数
    secretid:  config.secretId,
    appid: config.appId,
    // 实时识别接口参数
    engine_model_type : '16k_zh', // 因为内置WebRecorder采样16k的数据，所以参数 engineModelType 需要选择16k的引擎，为 '16k_zh'
    // 以下为非必填参数，可跟据业务自行修改
    voice_format : 1,
    hotword_id : '08003a00000000000000000000000000',
    needvad: 1,
    filter_dirty: 1,
    filter_modal: 2,
    filter_punc: 0,
    convert_num_mode : 1,
    word_info: 2
  }

  $('#start').on('click', function () {
    webAudioSpeechRecognizer = new WebAudioSpeechRecognizer(params);
    const areaDom = $('#recognizeText');
    areaDom.text('');
    let resultText = '';
    $(this).hide();
    $('#connecting').show();
    // 开始识别
    webAudioSpeechRecognizer.OnRecognitionStart = (res) => {
      console.log('开始识别', res);
    };
    // 一句话开始
    webAudioSpeechRecognizer.OnSentenceBegin = (res) => {
      console.log('一句话开始', res);
      isCanStop = true;
      $('#end').show();
      $('#recognizing').show();
      $('#connecting').hide();
    };
    // 识别变化时
    webAudioSpeechRecognizer.OnRecognitionResultChange = (res) => {
      console.log('识别变化时', res);
      const currentText = `${resultText}${res.result.voice_text_str}`;
      areaDom.text(currentText);
    };
    // 一句话结束
    webAudioSpeechRecognizer.OnSentenceEnd = (res) => {
      console.log('一句话结束', res);
      resultText += res.result.voice_text_str;
      areaDom.text(resultText);
    };
    // 识别结束
    webAudioSpeechRecognizer.OnRecognitionComplete = (res) => {
      console.log('识别结束', res);
    };
    // 识别错误
    webAudioSpeechRecognizer.OnError = (res) => {
      console.log('识别失败', res)
      // $('#end').hide();
      $('#recognizing').hide();
      $('#start').show();
      $('#connecting').hide();
    };
    webAudioSpeechRecognizer.start();
  });
  $('#end').on('click', function () {
    $(this).hide();
    $('#recognizing').hide();
    $('#start').show();
    if (isCanStop) {
      webAudioSpeechRecognizer.stop();
    }
  });
  // 语音服务
  window.addEventListener('message', (e) => {
    switch(e.data.cmd) {
      case 'start':
        setTimeout(()=>{
          if(webAudioSpeechRecognizer){
            webAudioSpeechRecognizer.stop();
          }
        },1000*10*60)
        if(webAudioSpeechRecognizer){
          webAudioSpeechRecognizer.stop();
        }
        webAudioSpeechRecognizer = new WebAudioSpeechRecognizer(params);
        const areaDom = $('#recognizeText');
        areaDom.text('');
        let resultText = '';
        // 开始识别
        webAudioSpeechRecognizer.OnRecognitionStart = (res) => {
          console.log('开始识别', res);
        };
        // 一句话开始
        webAudioSpeechRecognizer.OnSentenceBegin = (res) => {
          console.log('一句话开始', res);
          isCanStop = true;
        };
        // 识别变化时
        webAudioSpeechRecognizer.OnRecognitionResultChange = (res) => {
          console.log('识别变化时', res);
          const currentText = `${resultText}${res.result.voice_text_str}`;
          window.parent.postMessage({
            cmd: 'change',
            params: {
              currentText:currentText
            }
          }, "*")
        };
        // 一句话结束
        webAudioSpeechRecognizer.OnSentenceEnd = (res) => {
          console.log('一句话结束', res);
          resultText += res.result.voice_text_str;
          areaDom.text(resultText);
          window.parent.postMessage({
            cmd: 'change',
            params: {
              currentText:resultText
            }
          }, "*")
        };
        // 识别结束
        webAudioSpeechRecognizer.OnRecognitionComplete = (res) => {
          console.log('识别结束', res);
        };
        // 识别错误
        webAudioSpeechRecognizer.OnError = (res) => {
          console.log('识别失败', res)
        };
        webAudioSpeechRecognizer.start();
        break;
      case 'end':
        if (isCanStop) {
          webAudioSpeechRecognizer.stop();
        } 
        break;
      }
  }, false)
// 在线对话
window.addEventListener('message', (e) => {
  switch(e.data.cmd) {
    case 'chatstart':
      setTimeout(()=>{
        if(webAudioSpeechRecognizer2){
          webAudioSpeechRecognizer2.stop();
        }
      },1000*10*60)
      if(webAudioSpeechRecognizer2){
        webAudioSpeechRecognizer2.stop();
      }
      webAudioSpeechRecognizer2 = new WebAudioSpeechRecognizer(params);
      const areaDom = $('#recognizeText');
      areaDom.text('');
      let resultText = '';
      // 开始识别
      webAudioSpeechRecognizer2.OnRecognitionStart = (res) => {
        console.log('开始识别', res);
      };
      // 一句话开始
      webAudioSpeechRecognizer2.OnSentenceBegin = (res) => {
        console.log('一句话开始', res);
        isCanStop = true;
      };
      webAudioSpeechRecognizer2.OnRecognitionResultChange = (res) => {
        console.log('识别变化时', res.result.voice_text_str);
        const currentText = `${resultText}${res.result.voice_text_str}`;
        window.parent.postMessage({
          cmd: 'chatchange',
          params: {
            currentText:currentText
          }
        }, "*")
      };
      // 一句话结束
      webAudioSpeechRecognizer2.OnSentenceEnd = (res) => {
        console.log('一句话结束', res.result.voice_text_str);
        resultText += res.result.voice_text_str;
        areaDom.text(resultText);
        window.parent.postMessage({
          cmd: 'aWorldend',
          params: {
            currentText:resultText
          }
        }, "*")
        window.parent.postMessage({
          cmd: 'change',
          params: {
            currentText:resultText
          }
        }, "*")
        resultText=''
      };
      // 识别结束
      webAudioSpeechRecognizer2.OnRecognitionComplete = (res) => {
        console.log('识别结束', res);
        
      };
      // 识别错误
      webAudioSpeechRecognizer2.OnError = (res) => {
        console.log('识别失败', res)
        window.parent.postMessage({
          cmd: 'chatError',
          params: {
            currentText:res
          }
        }, "*")
      };
      webAudioSpeechRecognizer2.start();
      break;
    case 'chatend':
      // if (isCanStop) {
        console.log('停止识别')
        webAudioSpeechRecognizer2.stop();
        // webAudioSpeechRecognizer = null
      // } 
      break;
    }
}, false)
});

